<?php
include_once 'app/until/GFW_database.php';

class Db_user extends Model_Db_Core {

	protected  $con       = null;
	protected  $user      = null;
	protected  $user_id   = null;

	function __construct(){
	}
	public function LoginCheck($email, $password) {	
		$filter  = array('email' => $email,"password" => md5($password));
	    $select  = array("id");
	    
	    parent::getSQLResult(__CLASS__,$filter,$select);
	    $this->user = parent::result(1);

	    return $this->user;
		
	}
	public function GetUserByID($ID) {		
		$filter  = array('id' => $ID);
	    $select  = array();
	    
	    parent::getSQLResult(__CLASS__,$filter,$select);
	    $user =  parent::result(1);

	    return $user;		
	}
	public function GetUserByEmail($email) {		
		$filter  = array('email' => $email);
	    $select  = array();
	    
	    parent::getSQLResult(__CLASS__,$filter,$select);
	    $user = parent::result(1);

	    return $user;		
	}
	public function GetUserByName($Name) {		
		$filter  = array('username' => $Name);
	    $select  = array('id');
	    
	    parent::getSQLResult(__CLASS__,$filter,$select);
	    $user = parent::result(1);

	    return $user;		
	}		
    public function getSelectetDesign($user_id) {
        $filter  = array('id' => $user_id);
	    $select  = array("design");
	    
	    parent::getSQLResult(__CLASS__,$filter,$select);
	    $this->user = parent::result(1);

	    return $this->user;
	}
	public function Login(){
		if ($this->user_id)
		{
		    
		    parent::setInsert("Db_login_session");
		 	 	
    		$this->value['user_id'] = $this->user_id;
    		$this->value['login_date'] = "NOW()";
    		$this->value['login_till'] = "DATE_ADD(NOW(),INTERVAL 15 MINUTE)";
    		$this->value['ip_adress'] = $_SERVER['REMOTE_ADDR'];
    		$this->value['SID'] = session_id();
    				
    	    $status = $this->writeToDb();
    	   
    	    return $status;		    
		}
	}

	public function CreateUser($username,$email,$password,$bday,$sex = '',$code = '',$contry='Germany'){

	    parent::setInsert(__CLASS__);
		 	 	
		$this->value['username'] = $username;
		$this->value['password'] = md5($password);
		$this->value['email'] = $email;
		$this->value['sex'] = $sex;
		$this->value['bday'] = $bday;
		$this->value['code'] = $code;
		$this->value['contry'] = $contry;
		$this->value['design'] = 1;
				
	    $status = $this->writeToDb();
	   
	    return $status;
	}
	public function CheckSession(){		
	    $sql = 'Select id, SID From login_session Where SID = "'.session_id().'" and ip_adress = "'.$_SERVER['REMOTE_ADDR'].'" and login_till > NOW()';
	    
	    parent::setSqlManuel($sql);
	    $result = parent::result(1);
		 
		if ($result['id'])
		{
			$sql = "UPDATE login_session SET login_till = DATE_ADD(NOW(),INTERVAL 15 MINUTE) WHERE id =".$result['id'];
		    parent::setSqlManuel($sql);
		}
		else
		{
			unset($_SESSION["UserId"]);
			unset($_SESSION["UserName"]);
		}
		 
		return $result['SID'];
	}
    public function setDesign($user_id,$design){
        if($design == 0){
            $design = 'NULL';
        }
                
        parent::setInsert(__CLASS__);
        $this->where['id'] = $user_id;        
		$this->value['design'] = $design;
				
	    $status = $this->writeToDb(true);
	   
	    return $status;
    }
  	public function updatePic($user_id,$pfad50= "nopic.png",$pfad180= "nopic.png"){
        parent::setInsert(__CLASS__);
        
        $this->where['id'] = $user_id;     
        $this->value['bild_50'] = $pfad50;
		$this->value['bild_180'] = $pfad180;
				
	    $status = $this->writeToDb(true);
	   
	    return $status;
  	}
    public function aktivierung($code){
		$con = self::startGDB();

		$con->Select('Select * from user where aktivierung = "'.$code.'" ');
		$found = $con->numrows();
		 
		$con->Select('UPDATE `user` SET `aktivierung` = "0", status = "0" WHERE `aktivierung` = "'.$code.'"');

		if ($found > 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}

?>